<?php

class AdminAdvertiseController extends AdminController
{
  use ApiTrait;
  public static function __permissions()
  {
    return [
      'name' => '广告管理',
      'permissions' => [
        'advertise.list' => '查看广告列表',
        'advertise.edit' => '新增／编辑广告',
      ],
    ];
  }
  public function listAction()
  {
    if (!$this->account->checkPermission(['advertise.list'])) {
      $this->addErrorMessage('您未拥有相应权限！！');
      $this->gotoUrl('admin', true);
    }
    $advertiseModel = $this->getModel('advertise');
    if ($this->req->isPost()) {
      if (!$this->account->checkPermission(['advertise.edit'])) {
        $this->addErrorMessage('您未拥有相应权限！！');
        $this->gotoUrl('admin', true);
      }
      $name = $this->req->getPostTrim('name');
      $image = $this->req->getPostTrim('image');
      if (hasEmpty([$name, $image])) {
        $this->addErrorMessage('参数错误！！');
      } else if ($advertiseModel->createAdvertise([
        'name' => $name, 'user_id' => $this->account->user_id, 'image' => $image
      ])) {
        $this->addSuccessMessage('添加广告[' . $name . ']成功！！');
      } else {
       $this->addErrorMessage('添加广告[' . $name . ']失败，请重新添加！！');
      }
      $this->gotoUrl('admin/advertise/list', true);
    }
    $page = max(intval($this->req->getQuery('page', 1)), 1);
    $rows = 15;
    $query = [
      'search' => trim($this->req->getQuery('search', '')),
      'status' => trim($this->req->getQuery('status', '')),
    ];
    $view = $this->getView();
    $list = $advertiseModel->getAdvertisesList($query, $page, $rows);
    $count = $advertiseModel->getAdvertisesCount($query);
    if ($this->req->isAjax()) {
      return $this->okReturn([
        'list' => $list,
        'next' => count($list) == $rows ,
      ]);
    }
    $view->assign([
      'query' => $query,
      'list' => $list, 'count' => $count,
      'page' => $page, 'rows' => $rows
    ]);
    $view->display('admin/advertise/list.phtml');
  }

  public function advertiseUpdatedAction($op, $advertiseid)
  {
    if (!$this->account->checkPermission(['advertise.edit'])) {
      $this->addErrorMessage('您未拥有相应权限！！');
      $this->gotoUrl('admin', true);
    }
    $advertiseModel = $this->getModel('advertise');
    if (!($advertise = $advertiseModel->getAdvertise($advertiseid))) {
      $this->addErrorMessage('参数错误！！');
      $this->gotoUrl('admin/advertise/cate', true);
    } else if ($op == 'delete') {
      if ($advertise->delete()) {
        $this->addSuccessMessage('删除广告[' . $advertise->name .  '] 成功');
      } else {
        $this->addErrorMessage('删除广告[' . $advertise->name .  '] 失败，请重试');
      }
    } else if ($op == 'online') {
      $advertise->status = Advertise::IS_ONLINE;
      if ($advertise->update()) {
        $this->addSuccessMessage('广告[' . $advertise->name.  '] 上架成功');
      } else {
        $this->addErrorMessage('广告[' . $advertise->name.  '] 上架失败！！');
      }
    } else if ($op == 'offline') {
      $advertise->status = Advertise::IS_OFFLINE;
      if ($advertise->update()) {
        $this->addSuccessMessage('广告[' . $advertise->name.  '] 下架成功');
      } else {
        $this->addErrorMessage('广告[' . $advertise->name.  '] 下架失败！！');
      }
    }
    $this->gotoUrl('admin/advertise/cate', true);
  }

}
